<?php

namespace App\Http\Controllers\GuanyuanSSO\Web;

use App\Http\Controllers\BaseController;
use App\Service\GuanyuanSSO\GuanyuanAccountService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class GuanyuanAccountController extends BaseController
{

    /**
     * 添加账号
     * @param Request $request
     * @return JsonResponse
     */
    public function add(Request $request): JsonResponse
    {
        $this->validate(
            $request, [
                'guanyuanId' => 'required|string',
                'name'       => 'required|string',
                'desc'       => 'string',
            ]
        );
        $userId = getRmsUserId();
        return $this->success(GuanyuanAccountService::add($request, $userId));
    }

    /**
     * 编辑账号
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function update(Request $request, int $id): JsonResponse
    {
        $this->validate(
            $request, [
//                'guanyuanId' => 'required|string',
                'name'       => 'required|string',
                'desc'       => 'string',
            ]
        );
        $userId = getRmsUserId();
        GuanyuanAccountService::update($request, $id, $userId);
        return $this->success();
    }

    /**
     * 获取列表
     * @param Request $request
     * @return JsonResponse
     */
    public function list(Request $request)
    {
        $username   = (string)$request->get('username', '');
        $mobile     = (string)$request->get('mobile', '');
        $guanyuanId = (string)$request->get('guanyuanId', '');
        $pageSize   = (int)$request->get('pageSize', 20);
        $pageNum    = (int)$request->get('pageNum', 1);
        return $this->success(GuanyuanAccountService::getList($username, $mobile, $guanyuanId, $pageSize, $pageNum));
    }

    /**
     * 查询信息
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function info(Request $request, int $id): JsonResponse
    {
        return $this->success(GuanyuanAccountService::info($id));
    }

    /**
     * 删除
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function del(Request $request, int $id): JsonResponse
    {
        $userId = getRmsUserId();
        GuanyuanAccountService::del($id, $userId);
        return $this->success();
    }


    /**
     * 添加绑定
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function bindAdd(Request $request, int $id): JsonResponse
    {
        $this->validate(
            $request, [
                'fsUserId' => 'array',
            ]
        );
        $userId = getRmsUserId();
        GuanyuanAccountService::bindAdd($request, $id, $userId);
        return $this->success();
    }


    /**
     * 获取飞书一级部门下用户
     * @param Request $request
     * @return JsonResponse
     */
    public function fsUserList(Request $request): JsonResponse
    {
        return $this->success(GuanyuanAccountService::fsUserList());
    }


    /**
     * 获取已绑定的用户
     * @param Request $request
     * @return JsonResponse
     */
    public function getBindFsUser(Request $request): JsonResponse
    {
        return $this->success(GuanyuanAccountService::getBindFsUser());
    }

}
